/*
 * @lc app=leetcode.cn id=415 lang=cpp
 *
 * [415] 字符串相加
 */

// @lc code=start
class Solution
{
public:
  string addStrings(string num1, string num2)
  {
    int m = num1.size(), n = num2.size();
    int i = m - 1, j = n - 1, r = 0;
    string ans;
    for (; i >= 0 && j >= 0; --i, --j) {
      int sum = ((num1[i] - '0') + (num2[j] - '0')) + r;
      r = sum / 10;
      ans.push_back(sum % 10 + '0');
    }
    for (; i >= 0; --i) {
      int sum = num1[i] - '0' + r;
      r = sum / 10;
      ans.push_back(sum % 10 + '0');
    }
    for (; j >= 0; --j) {
      int sum = num2[j] - '0' + r;
      r = sum / 10;
      ans.push_back(sum % 10 + '0');
    }
    if (r == 1)
      ans.push_back(1 + '0');
    reverse(ans.begin(), ans.end());
    return ans;
  }
};
// @lc code=end
